<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>

        <el-card>
            <el-row>
                <el-col>
                    <el-button type="primary" @click="addCate()">添加分类</el-button>
                </el-col>
            </el-row>

            <!--表格-->
            <tree-table :data="cateList"
                        :columns="columns"
                        :selection-type="false"
                        :expand-type="false"
                        show-index
                        border
                        class="treeTable"
            >
                <template slot="isok" slot-scope="scope">
                    <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color: lightgreen"></i>
                    <i class="el-icon-error" v-else style="color: red"></i>
                </template>

                <template slot="order" slot-scope="scope">
                    <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
                    <el-tag type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
                    <el-tag type="warning" v-else>三级</el-tag>
                </template>

                <template slot-scope="scope" slot="edit">
                    <el-button type="primary" icon="el-icon-edit">编辑</el-button>
                    <el-button type="danger" icon="el-icon-delete">删除</el-button>
                </template>
            </tree-table>

            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryInfo.pagenum"
                    :page-sizes="[3, 5, 10, 15]"
                    :page-size="queryInfo.pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </el-card>


        <el-dialog
                @close="deleteDia"
                title="添加分类"
                :visible.sync="addCateDialogVisible"
                width="50%"
                >
            <el-form :model="queryCate" :rules="queryCateRules" ref="queryCateRef" label-width="100px">
                <el-form-item label="分类名称" prop="cat_name">
                    <el-input v-model="queryCate.cat_name"></el-input>
                </el-form-item>
                <el-form-item label="父级分类">
                    <el-cascader
                            change-on-select
                            clearable
                            expand-trigger="hover"
                            v-model="selectedKeys"
                            :options="parentCateList"
                            :props="cascaderProps"
                            @change="parentCateChanged"></el-cascader>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addCateDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addCateList">确 定</el-button>
            </span>
        </el-dialog>

    </div>

</template>

<script>
    import { getCategories,addCategories } from "../api/user";

    export default {
        name: "Categories",
        data() {
            return {
                selectedKeys: [],
                cascaderProps: {
                    value: 'cat_id',
                    label: 'cat_name',
                    children: 'children'
                },
                parentCateList: [],
                queryCateRules: {
                    cat_name: [ { required: true, message: '请输入分类名称', trigger: 'blur' }]
                },
                queryCate: {
                    cat_name: '',
                    cat_pid: 0,
                    cat_level: 0
                },
                addCateDialogVisible: false,
                cateList: [],
                total: '',
                queryInfo: {
                    type: 3,
                    pagenum: 1,
                    pagesize: 5
                },
                columns: [
                    {
                        label: '分类名称',
                        prop: 'cat_name'
                    },
                    {
                        label: '是否有效',
                        type: 'template',
                        template: 'isok'
                    },
                    {
                        label: '排序',
                        type: 'template',
                        template: 'order'
                    },
                    {
                        label: '操作',
                        type: 'template',
                        template: 'edit'
                    }
                ]
            }
        },
        created() {
            this.getCateList()
        },
        methods: {
            deleteDia() {
                this.$refs.queryCateRef.resetFields()
                this.selectedKeys = []
                this.queryCate.cat_pid = 0
                this.queryCate.cat_level = 0
            },
            addCateList() {
                console.log(this.queryCate)

            },
            parentCateChanged() {
              if (this.selectedKeys.length > 0) {
                  this.queryCate.cat_pid = this.selectedKeys[this.selectedKeys.length -1]
                  this.queryCate.cat_level = this.selectedKeys.length
              } else {
                  this.queryCate.cat_pid = 0
                  this.queryCate.cat_level = 0
              }
            },
            async getParentCateList() {
                const res = await getCategories({params: {type: 2}})
                if (res.data.meta.status === 200) {
                    this.parentCateList = res.data.data
                }
            },
            addCate() {
                this.getParentCateList()
                this.addCateDialogVisible = true
            },
            handleCurrentChange(newPage) {
                this.queryInfo.pagenum = newPage
                this.getCateList()
            },
            handleSizeChange(newSize) {
                this.queryInfo.pagesize = newSize
                this.getCateList()
            },
            async getCateList() {
                const res = await getCategories(this.queryInfo)
                if (res.data.meta.status === 200) {
                    this.cateList = res.data.data.result
                    this.total = res.data.data.total
                }
                console.log(res)
            }
        }
    }
</script>

<style scoped>
    .el-card {
        margin-top: 20px;
    }
    .treeTable {
        margin-top: 20px;
    }

    .el-cascader {
        width: 100%;
    }

</style>
